1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
package com.chapter03Jedis;
import com.google.common.base.Joiner; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Test; import redis.clients.jedis.*;
import java.util.*;
public class JedisTest { @Test public void testJedisCluster() { Set<HostAndPort> nodeList = new HashSet<>(); nodeList.add(new HostAndPort("127.0.0.1" , 7000) ); nodeList.add(new HostAndPort("127.0.0.1" , 7001) ); nodeList.add(new HostAndPort("127.0.0.1" , 7002) ); nodeList.add(new HostAndPort("127.0.0.1" , 7003) ); nodeList.add(new HostAndPort("127.0.0.1" , 7004) ); nodeList.add(new HostAndPort("127.0.0.1" , 7005) ); JedisPoolConfig poolConfig = new JedisPoolConfig(); int timeout = 30_000; JedisCluster jedisCluster = new JedisCluster(nodeList , timeout , poolConfig); jedisCluster.set("hello" , "world"); System.out.println(jedisCluster.get("hello"));
}
@Test public void testConnectRedis() { Jedis jedis = new Jedis("127.0.0.1" , 6379); System.out.println(jedis.ping()); jedis.set("hello" , "world"); System.out.println(jedis.get("hello")); }
@Test public void testJedisPool() { JedisPoolConfig poolConfig = new JedisPoolConfig(); JedisPool jedisPool = new JedisPool(poolConfig , "127.0.0.1" , 6379); Jedis jedis = jedisPool.getResource(); System.out.println(jedis.ping()); jedis.set("hello" , "world"); System.out.println(jedis.get("hello")); }
@Test public void testWithoutPipeline() { Jedis jedis = new Jedis("127.0.0.1" , 6379); for(int i = 0; i < 10000 ; i++ ) { jedis.hset("hashKey-" + i , "field-" + i , "value-" + i); } }
@Test public void testPipeline() { Jedis jedis = new Jedis("127.0.0.1" , 6379); for(int i = 0 ; i < 100 ; i++ ) { Pipeline pipeline = jedis.pipelined(); for(int j = i * 100 ; i < (i+1) * 100 ; j++ ) { pipeline.hset("hashKey-" + i , "field-" + i , "value-" + i); } pipeline.syncAndReturnAll(); } }
@Test public void testSubscribe() { Jedis jedis = new Jedis("127.0.0.1" , 6379); jedis.subscribe(new JedisPubSub() { @Override public void onMessage(String channel, String message) { System.out.println("receive channel = [" + channel + "] message = [" + message + "]"); } } , "aliTV" , "googleTV"); }
@Test public void testPublish() { Jedis jedis = new Jedis("127.0.0.1" , 6379); jedis.publish("aliTV" , "I am xuyinan"); jedis.publish("googleTV" , "My age is 27"); }
@Test public void testSentinelPool() { Set<String> sentinelSet = new HashSet<String>() {{ add("127.0.0.1:26379"); add("127.0.0.1:26380"); add("127.0.0.1:26381"); }}; JedisPoolConfig poolConfig = new JedisPoolConfig(); String masterName = "myMaster"; int timeout = 30_000; JedisSentinelPool sentinelPool = new JedisSentinelPool(masterName , sentinelSet , poolConfig , timeout); Jedis jedis = sentinelPool.getResource(); jedis.set("hello" , "world"); System.out.println(jedis.get("hello")); jedis.close(); }
}
|